package com.yangjun.service.departmentCustomization;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
import com.yangjun.utils.DvUtils;
/**
 * 
 * @ClassName PreSalesPharmacistService
 * @Description 可视化部门定制售前科组药师页面的内容
 * @author Administrator
 * @Date 2017年3月15日 上午10:35:01
 * @version 1.0.0
 */
@Service
public class PreSalesPharmacistService extends DepartmentCustomizationCommonService{
	
	/**
	 * @Description 根据科组名查询排行
	 * @param groupName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月15日 上午10:36:09
	 */
	public Map<String,List<Map<String,Object>>> queryCompletionRateByStaff(String groupName){
		Map<String,List<Map<String,Object>>> resultMap = Maps.newHashMap();
		//完成率排名
		List<Map<String,Object>> taskDoneRateRankList = Lists.newArrayList();
		//客单价排名
		List<Map<String,Object>> salePctRankList = Lists.newArrayList();
		
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = this.preSalesSectionMapper.queryCompletionRateByStaff(groupName);
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				String staffName = preSalesSectionOverViewEntity.getStaffName();
				
				Map<String,Object> taskDoneRateRankItemMap = Maps.newHashMap();
				taskDoneRateRankItemMap.put("name", staffName);
				taskDoneRateRankItemMap.put("value", String.valueOf(preSalesSectionOverViewEntity.getDoneRate()));
				 
				Map<String,Object> salePctRankItemMap = Maps.newHashMap();
				salePctRankItemMap.put("name", staffName);
				salePctRankItemMap.put("value", String.valueOf(preSalesSectionOverViewEntity.getSalePct()));
				
				taskDoneRateRankList.add(taskDoneRateRankItemMap);
				salePctRankList.add(salePctRankItemMap);
			}
		}
		//按照值排序
		Collections.sort(taskDoneRateRankList,new FloatComparatorSortList("value"));
		Collections.sort(salePctRankList,new FloatComparatorSortList("value"));
		
		resultMap.put("taskDoneRateRank",taskDoneRateRankList);
		resultMap.put("salePctRank", salePctRankList);
		return resultMap;
	}

	public List<Object[]> queryStaffSalesTable(String groupName,String sourceName,String roomName,String staffName){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = this.preSalesSectionMapper.queryStaffSalesTable(groupName,sourceName,roomName,staffName);
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				
				String name = preSalesSectionOverViewEntity.getStaffName();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int validAdviceCnt= preSalesSectionOverViewEntity.getValidAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				float salePct = preSalesSectionOverViewEntity.getSalePct();
				int nopayOrderCnt = preSalesSectionOverViewEntity.getNopayOrderCnt();
				float nopayOrderAmount = preSalesSectionOverViewEntity.getNopayOrderAmount();
				int totalTask = preSalesSectionOverViewEntity.getTotalTask();
				float doneRate = preSalesSectionOverViewEntity.getDoneRate();
				int workDays = preSalesSectionOverViewEntity.getWorkDays();
				int tslPerDay = preSalesSectionOverViewEntity.getTskPerDay();
				Object[] dataArr = new Object[]{name,adviceCnt,validAdviceCnt,orderCnt,salesOrderAmount,salePct,nopayOrderCnt,nopayOrderAmount,totalTask,doneRate,workDays,tslPerDay};
				if(arrIsExistNull(1,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		DvUtils.PinYinComparatorSortList(resultList);
		return resultList;
	}
	public List<Object[]> queryStaffSalesTableByChannel(String groupName,String sourceName,String roomName,String staffName){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = this.preSalesSectionMapper.queryStaffSalesTableByChannel(groupName,sourceName,roomName,staffName);
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				String channelName = preSalesSectionOverViewEntity.getChannel();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int validAdviceCnt= preSalesSectionOverViewEntity.getValidAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				float salePct = preSalesSectionOverViewEntity.getSalePct();
				int nopayOrderCnt = preSalesSectionOverViewEntity.getNopayOrderCnt();
				float nopayOrderAmount = preSalesSectionOverViewEntity.getNopayOrderAmount();
				Object[] dataArr = new Object[]{channelName,adviceCnt,validAdviceCnt,orderCnt,salesOrderAmount,salePct,nopayOrderCnt,nopayOrderAmount};
				if(arrIsExistNull(1,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		return resultList;
	}
	public Map<String,Object> queryPharmacistComparisonLineChartByHour(String groupName,String sourceName,String roomName,String staffName){
		String currentDate = sdfDay.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		String contrastDate = sdfDay.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		List<PreSalesSectionOverViewEntity> LineChartByHourList = preSalesSectionMapper.queryPharmacistComparisonLineChartByHour(groupName,sourceName,roomName,staffName,dateArr);
		int index = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		return queryLineChartData(DVConstants.HOUR_ARR,LineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}

	public Map<String,Object> queryPharmacistComparisonLineChartByDayofWeek(String groupName,String sourceName,String roomName,String staffName){
		String currentDate = sdfDay.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.WEEK_OF_MONTH, -1);
		String contrastDate = sdfDay.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询某两天的数据
		List<PreSalesSectionOverViewEntity> LineChartByHourList = preSalesSectionMapper.queryPharmacistComparisonLineChartByHour(groupName,sourceName,roomName,staffName,dateArr);
		int index = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		return queryLineChartData(DVConstants.HOUR_ARR,LineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	
	public Map<String,Object> queryPharmacistComparisonLineChartByDay(String groupName,String sourceName,String roomName,String staffName){
		String currentDate = sdfMonth.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		String contrastDate = sdfMonth.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> LineChartByHourList = preSalesSectionMapper.queryPharmacistComparisonLineChartByDay(groupName,sourceName,roomName,staffName,dateArr);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		return queryLineChartData(DVConstants.MONTH_DAY,LineChartByHourList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	
	
	public Map<String,Object> queryPharmacistComparisonLineChartByWeek(String groupName,String sourceName,String roomName,String staffName){
		Calendar calendar = Calendar.getInstance();
		String currentDate = sdfDay.format(new Date());
		calendar.setTime(new Date());
		int dayOfWeek= calendar.get(Calendar.DAY_OF_WEEK)-1;
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfWeek+6));
		String contrastDate = sdfDay.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, +6);
		Date tmpDate = calendar.getTime();
		//一次性查询两周的数据
		List<PreSalesSectionOverViewEntity> todayLineChartByHourList = setDayOfWeek(preSalesSectionMapper.queryPharmacistComparisonLineChartByWeek(groupName,sourceName,roomName,staffName,currentDate,contrastDate),tmpDate);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
		return queryLineChartData(DVConstants.WEEK_NAME_ARR,todayLineChartByHourList,new String[]{"本周","上周"},DVConstants.COMPARISON_LEGEND,index);
	}
	public Map<String,List<String[]>> queryPharmacistTableDataByDay(String groupName,String sourceName,String roomName,String staffName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.MONTH, -1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryPharmacistTableData(groupName,sourceName,roomName,staffName,beginDate,endDate,null);
		return queryTableData(tableByDayList,DVConstants.HOUR_ARR,"channel","day");
	}
	public Map<String,List<String[]>> queryPharmacistTableDataByMonth(String groupName,String sourceName,String roomName,String staffName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询12个月的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryPharmacistTableData(groupName,sourceName,roomName,staffName,beginDate,endDate,"MONTH");
		return queryTableData(tableByDayList,DVConstants.MONTH_DAY,"channel","month");
	}
	public Map<String,List<String[]>> queryPharmacistTableDataByYear(String groupName,String sourceName,String roomName,String staffName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_YEAR,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两年的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryPharmacistTableData(groupName,sourceName,roomName,staffName,beginDate,endDate,"YEAR");
		return queryTableData(tableByDayList,DVConstants.MONTH,"channel","year");
	}
	
}
